macromedia Your Account International Help
Products Support & Training DevNet Solutions Partners Downloads Store
Products Support & Training DevNet Solutions Partners Downloads Store
Home > Products > Director > Support > TechNote Index
Macromedia Director Support Center - TechNote

Conditional statements: how to use "case" statements

A conditional statement is used to test the value of an expression and perform an action based on the result. A real world example of a conditional statement could be "If it is raining, I should bring my umbrella". The condition is whether or not it is raining. The action is whether or not I bring my umbrella. Conditional statements are invaluable when testing for specific scenarios in Director. For example, a conditional statement can test to see if a QuickTime movie is playing, and if it is not the Director movie will jump to a different frame.

'If' statements are most often used to test one condition. 'Case' statements can test for two or more conditions. For more information about if statements, see Conditional statements: how to use "if" statements (TechNote 16207).

Case Statements
Case statements are like if statements, but they are used to test against several expressions rather than one. For example,
a case statement can be used to test which key a user has pressed. A script is written that executes every time a user clicks on a key, and if the key equals a key in the case statement, an action occurs. To see an example of a case statement that test which key is pressed, see "case" in the Lingo dictionary, and How do I use KeyCode? (TechNote 15887).

Like 'if' statements, case statements have a specific structure. First the expression being tested is declared. Then the value or condition of that expression is compared to several other values or conditions.

The script below is attached to the button in the sample movie labeled "testing the value of x using a case statement" . A case statement is used to trigger an alert box depending on the value of a global variable. Once the variable is created and a value is set, the script can be added to a button sprite.

This script first declares the global variable. Then on a mouseUp event, a case statement is used to make an alert box appear on screen. After the word "case", the expression that is being tested (x) is entered. The word 'of' follows.

The information on the left hand side of the expression shows what the main expression is being compared to. The first expression is 1, the second expression is 2, and so on. The information to the right of the expression is the action that will take place if that expression is true. So, for example, if x is equal to 3, an alert box will appear on the screen with the text "x=3". If x does not equal any of these expressions, nothing will happen.

global x
on mouseup
case (x) of
1:alert"x=1"
2:
alert"x=2"
3:
alert"x=3"
4:
alert"x=4"
end case
end

A translation of the above code could be as follows:
If x=1 then alert "x=1"
If x=2 then alert "x=2"
If x=3 then alert "x=3"
If x=4 then alert "x=4"

Resources
See Global variables (TechNote 14225) for more information about global variables and how they work.

Lingo
The following lingo commands were used to create the functionality in this sample movie. Please refer to the Lingo dictionary for more information about these terms: case, global, alert

Developer Notes
The sample file includes code comments and a cast member named "Developer Notes" which contains implementation details.

Note: This is just one of the many ways to create this functionality in Director.
This download contains the following file:

> case_statements.dir

Download the Windows zip file

ID: 16228
Product: Director
Versions: All
OS: N/A
Browser: N/A
Server: N/A
Database: N/A
Former ID: N/A
How useful was this document?
less more

1

2

3

4

5

How can the document be improved? (300 characters or less - you will not receive a reply.)

Last updated: February 24, 2003
Created: March 13, 2002